package ua.tyran53red;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Start extends JFrame{
	private static final long serialVersionUID = 1L;
	private JLabel lblUserName = new JLabel("User name:");
	private JLabel lblUserPassword = new JLabel("Password:");
	private JTextField txtUserName = new JTextField();
	private JPasswordField txpUserPassword = new JPasswordField();
	private JButton bttLogin = new JButton("Log in");

	public Start(String title, int width, int height) {
		ActionListener actionListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!"".equals(txtUserName.getText())) {
					setVisible(false);
					StartDesktop.getInstance("Chat", txtUserName.getText());
				}
			}
		};
		setTitle(title);
		setSize(width, height);
		setLocation((int) Math.round((Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - (width / 2)),
				    (int) Math.round((Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2) - (height / 2)));
		setLayout(null);
		setVisible(true);
		setResizable(false);
		lblUserName.setBounds(25, 25, 100, 25);
		lblUserPassword.setBounds(25, 55, 100, 25);
		txtUserName.setBounds(125, 25, 100, 25);
		txpUserPassword.setBounds(125, 55, 100, 25);
		bttLogin.setBounds(150, 85, 75, 25);
		bttLogin.addActionListener(actionListener);
		txtUserName.addActionListener(actionListener);
		txpUserPassword.addActionListener(actionListener);
		add(lblUserName);
		add(lblUserPassword);
		add(txtUserName);
		add(txpUserPassword);
		add(bttLogin);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	
	public static void main(String[] args) {
		new Start("Log in", 250, 150);
	}
}
